import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    redirect: '/home/student',
    children: [
      {
        path: 'student',
        name: 'CurrencyList',
        component: () => import('../views/CurrencyList.vue'),
        meta: { title: '币种列表' }
      },
      {
        path: 'add',
        name: 'AddCurrency',
        component: () => import('../views/AddCurrency.vue'),
        meta: { title: '添加币种' }
      },
      {
        path: 'edit/:id',
        name: 'EditCurrency',
        component: () => import('../views/EditCurrency.vue'),
        meta: { title: '编辑币种' }
      },
      {
        path: 'warning',
        name: 'Warning',
        component: () => import('../views/Warning.vue'),
        meta: { title: '预警统计' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 简单的登录验证，实际项目中应该使用更复杂的验证方式
  if (to.path !== '/login') {
    const token = localStorage.getItem('token')
    if (!token) {
      next('/login')
      return
    }
  }
  next()
})

export default router